<?php
namespace jy_shopnewfee;
defined('BY_JYA') or exit('error');
/*
	分销佣金
	
*/
class commission extends \plugin{
	/*
		生成佣金
	*/
	function create($order){
		$this->order=$order;
		$this->order['onegoodsnum']=count($this->order['goods']);
		$member=$this->get('member',array('mid'=>$order['mid']));
		$this->agent_id=$member['agent_id'];
		//自购升级
		if($member['is_agent']){
			$this->update('member',array('myorder_price +='=>$order['price']),array('mid'=>$order['mid']));
			$this->model('member')->update_agent_level($order['mid']);
		}
		$this->commission_order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$this->order['mid'],
			'ordersn'=>$this->order['ordersn'],
			'order_price'=>$this->order['price'],
			'plugin_name'=>$this->order['plugin_name']?:'',
			'commission_money'=>0,
			'jicha_money'=>0,
			'createtime'=>TIMESTAMP,
		);
		$this->memberfee=array();
		$maxlevel=$this->get('level',array('uniacid'=>$_SESSION['uniacid']),'','level desc');
		$this->commission_order['maxlevel_id']=$maxlevel=$maxlevel['id'];
		$this->lasttype=$member['type'];
		$this->lastlevel_id=$member['level_id'];
		$this->last_mid=$member['mid'];
		$this->lastpaixu=$member['paixu'];
		$this->is_create_boss_d='';
		$this->qu_fee2=0;
		$this->qu_fee9=0;
		if(!$this->agent_id){
			return;
		}
		//直推-销售提成1（A、B）
		$agent=$agent0=$this->get('member',array('mid'=>$this->agent_id));
		$level=$this->get('level',array('id'=>$agent['level_id'],'uniacid'=>$_SESSION['uniacid']));
		$level['commissions']=json_decode($level['commissions'],true);
		$level0=$level;
		//代理才有提成1
		if(!$agent['type']){
			$this->create_commission($agent,$level,4);
		}
		//BOSS以上身份才享受,第三个开始
		if($agent['type'] && $this->lastpaixu>2){
			//直推-销售提成3（C以后）
			$this->create_commission($agent,$level,6);
		}
		//自购分红池
		if($member['type']>=2){
			//分红池（自购）
			$this->create_gu2_red($member);
		}
		for($i=0;$i<18;$i++){
			if(!$this->agent_id){
				break;
			}
			if($i>0){
				$agent=$this->get('member',array('mid'=>$this->agent_id));
				$level=$this->get('level',array('id'=>$agent['level_id'],'uniacid'=>$_SESSION['uniacid']));
				$level['commissions']=json_decode($level['commissions'],true);
			}		
			if(!$this->is_create_boss_d && $agent['type']>=1 && $this->lastpaixu<=2 && $i>0){
				//销售提成4（见点）上上级前两位
				$this->create_boss_d($agent,$level,7,$i);
			}
			//直推-销售提成2（A、B）
			if($this->lastpaixu<=2 && $i>0){
				$this->create_commission($agent0,$level0,5);
			}
			if($agent['type']>=5 && $this->lastltype<$agent['type']){
				//重消补助（极差）
				$this->create_qu_bufee($agent,2);
				//加盟基金报单补助（级差）
				$this->create_qu_bufee($agent,9);
			}
			if($agent['type']>=2 && $agent['type']<=4 && $this->lastltype<$agent['type']){
				//管理基金（级差）
				$this->create_gu2_c($agent);
			}
			if($agent['type']>=2){
				//（教育基金）平级
				$this->create_gu2_d($agent);
			}
			$this->lastltype=$agent['type'];
			$this->lastlevel_id=$agent['level_id'];
			$this->last_mid=$agent['mid'];
			$this->lastpaixu=$agent['paixu'];
			$this->agent_id=$agent['agent_id'];
		}
		//生成分销订单
		$indata=$this->commission_order;
		unset($indata['maxlevel_id']);
		unset($indata['jicha_money']);
		$this->insert('order',$indata);
		return true;
	}
	/*
		4直推-销售提成1（A、B），5销售提成2（A、B），6销售提成3（C以后）
	*/
	function create_commission($member,$level=array(),$type=4){
		$agent_id=$member['mid'];
		if(!$member || !$member['is_agent'] || $member['stop']){
			return $member['agent_id'];
		}
		if($type==4){
			$rate=$level['commissions']['commission1'];
		}elseif($type==5){
			$rate=$level['commissions']['commission2'];
		}elseif($type==6){
			$rate=$level['commissions']['boss_c'];
		}
		if(!$rate){
			return $member['agent_id'];
		}
		$money=0;
		$order_price=$this->commission_order['commission_price'];
		$remark=array(
				'level_id'=>$level_id,
				'istwo'=>$istwo,
				'order_price'=>$order_price,
				'goods_commission'=>array(),
		);	
		$this->order_price=0;
		$commission_pv=0;
		foreach($this->order['goods'] as $goods){
				$goods['api_data']=json_decode($goods['api_data'],true);
				$pv=$goods['api_data']['pv'];
				$goods['total']=$goods['total']?:1;
				$commission_pv +=$pv*$goods['total'];
		}
		if($type==5){
			$commission_pv=$this->fee_type4+$this->fee_type6+$this->fee_type7;
		}
		if(substr($rate,-1)=='%'){
			$goods_pvmoney=($commission_pv*substr($rate,0,-1))/100;
			$money +=$goods_pvmoney;
		}else{
			$money +=$rate;
		}
		$remark['commission_money']=$money;
		if($money>0){
				$this->commission_order['commission_money']+=$money;
				if($type==4){
					$this->fee_type4=$money;
				}elseif($type==5){
					$this->fee_type5=$money;
				}elseif($type==6){
					$this->fee_type6=$money;
				}
				$money=$this->up_fufee($member['mid'],$money);
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$member['mid'],
					'level'=>1,
					'ordersn'=>$this->order['ordersn'],
					'money'=>$money,
					'remark'=>ijson_encode($remark),
					'createtime'=>TIMESTAMP,
					'status'=>1,
					'type'=>$type,
				);
				$this->insert('commission',$new_commission);
		}
		return $member['agent_id'];
	}
	/*
		//销售提成4（见点）
	*/
	function create_boss_d($member,$level=array(),$type=7,$i=0){
		$agent_id=$member['mid'];
		if(!$member || !$member['is_agent'] || $member['stop']){
			return $member['agent_id'];
		}
		if($type==7){
			$rate=$level['commissions']['boss_d'];
		}elseif($type==5){
			$rate=$level['commissions']['commission2'];
		}elseif($type==6){
			$rate=$level['commissions']['boss_c'];
		}
		if(!$rate){
			return $member['agent_id'];
		}
		if($member['type']!=1){
			return $member['agent_id'];
		}
		$money=0;
		$order_price=$this->commission_order['commission_price'];
		$remark=array(
				'level_id'=>$member['level_id'],
				'order_price'=>$order_price,
				'goods_commission'=>array(),
		);	
		$this->order_price=0;
		$commission_pv=0;
		foreach($this->order['goods'] as $goods){
				$goods['api_data']=json_decode($goods['api_data'],true);
				$pv=$goods['api_data']['pv'];
				$goods['total']=$goods['total']?:1;
				$commission_pv +=$pv*$goods['total'];
		}
		if(substr($rate,-1)=='%'){
			$goods_pvmoney=($commission_pv*substr($rate,0,-1))/100;
			$money +=$goods_pvmoney;
		}else{
			$money +=$rate;
		}
		$remark['commission_money']=$money;
		if($money>0){
				$this->commission_order['commission_money']+=$money;
				if(!$i){
					$this->fee_type7=$money;
				}
				$money=$this->up_fufee($member['mid'],$money);
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$member['mid'],
					'level'=>1,
					'ordersn'=>$this->order['ordersn'],
					'money'=>$money,
					'remark'=>ijson_encode($remark),
					'createtime'=>TIMESTAMP,
					'status'=>1,
					'type'=>$type,
				);
				$this->insert('commission',$new_commission);
				//培训补助（平级）
				$this->create_boss_e($agent_id);
		}
		$this->is_create_boss_d=true;
		return false;
	}
	/*
		培训补助（平级）
	*/
	public function create_boss_e($agent_id,$money=0){
		$agent=$this->get('member',array('mid'=>$agent_id));
		if($agent['type']!=1){
			return false;
		}
		if($money){
			return false;
		}
		$level=$this->get('level',array('id'=>$agent['level_id'],'uniacid'=>$_SESSION['uniacid']));
		$rate=$level['commissions']['boss_e'];
		if(substr($rate,-1)=='%'){
			$fee=($money*substr($rate,0,-1))/100;
		}else{
			$fee=$rate;
		}
		if($fee>0){
				$fee=$this->up_fufee($agent['mid'],$fee);
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$agent['mid'],
					'level'=>1,
					'ordersn'=>$this->order['ordersn'],
					'money'=>$fee,
					'remark'=>ijson_encode($remark),
					'createtime'=>TIMESTAMP,
					'status'=>1,
					'type'=>8,
				);
				$this->insert('commission',$new_commission);
		}
		return true;
	}
	/*
		加盟基金,重消补助
	*/
	public function create_qu_bufee($member,$type=9){
		$agent_id=$member['mid'];
		if(!$member || !$member['is_agent'] || $member['stop']){
			return $member['agent_id'];
		}
		if($type==9){
			if($member['type']==5){
				$rate=$this->setting['qu_bufee'];
			}elseif($member['type']==6){
				$rate=$this->setting['shi_bufee'];
			}elseif($member['type']==7){
				$rate=$this->setting['sheng_bufee'];
			}
		}elseif($type==2){
			if($member['type']==5){
				$rate=$this->setting['qu_jicha'];
			}elseif($member['type']==6){
				$rate=$this->setting['shi_jicha'];
			}elseif($member['type']==7){
				$rate=$this->setting['sheng_jicha'];
			}
		}
		if(!$rate){
			return $member['agent_id'];
		}
		$money=0;
		$order_price=$this->commission_order['commission_price'];
		$remark=array(
				'order_price'=>$order_price,
				'goods_commission'=>array(),
		);	
		$this->order_price=0;
		$commission_pv=0;
		foreach($this->order['goods'] as $goods){
				$goods['api_data']=json_decode($goods['api_data'],true);
				$pv=$goods['api_data']['pv'];
				$goods['total']=$goods['total']?:1;
				$commission_pv +=$pv*$goods['total'];
		}
		if(substr($rate,-1)=='%'){
			$goods_pvmoney=($commission_pv*substr($rate,0,-1))/100;
			$money +=$goods_pvmoney;
		}else{
			$money +=$rate;
		}
		if($type==9){
			$money=$money-$this->qu_fee9;
			$this->qu_fee9 +=$money;
		}elseif($type==2){
			$money=$money-$this->qu_fee2;
			$this->qu_fee2 +=$money;
			$this->last_3fee2=$money;
		}
		$remark['commission_money']=$money;
		if($money>0){
				$money=$this->up_fufee($member['mid'],$money);
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$member['mid'],
					'level'=>1,
					'ordersn'=>$this->order['ordersn'],
					'money'=>$money,
					'remark'=>ijson_encode($remark),
					'createtime'=>TIMESTAMP,
					'status'=>1,
					'type'=>$type,
				);
				$this->insert('commission',$new_commission);
		}
		return true;
	}
	/*
		管理基金（级差）
	*/
	public function create_gu2_c($member,$type=1){
		$agent_id=$member['mid'];
		if(!$member || !$member['is_agent'] || $member['stop']){
			return $member['agent_id'];
		}		
		if($type==1){
			if($member['type']==2){
				$rate=$this->setting['gu2_c'];
			}elseif($member['type']==3){
				$rate=$this->setting['gu3_c'];
			}elseif($member['type']==4){
				$rate=$this->setting['gu4_c'];
			}
		}
		if(!$rate){
			return $member['agent_id'];
		}
		$money=0;
		$order_price=$this->commission_order['commission_price'];
		$remark=array(
				'order_price'=>$order_price,
				'goods_commission'=>array(),
		);	
		$this->order_price=0;
		$commission_pv=0;
		foreach($this->order['goods'] as $goods){
				$goods['api_data']=json_decode($goods['api_data'],true);
				$pv=$goods['api_data']['pv'];
				$goods['total']=$goods['total']?:1;
				$commission_pv +=$pv*$goods['total'];
		}
		if(substr($rate,-1)=='%'){
			$goods_pvmoney=($commission_pv*substr($rate,0,-1))/100;
			$money +=$goods_pvmoney;
		}else{
			$money +=$rate;
		}
		if($type==1){
			$money=$money-$this->gu_fee1;
			$this->gu_fee1 +=$money;
			$this->last_3fee1=$money;
		}
		$remark['commission_money']=$money;
		if($money>0){
				$money=$this->up_fufee($member['mid'],$money);
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$member['mid'],
					'level'=>1,
					'ordersn'=>$this->order['ordersn'],
					'money'=>$money,
					'remark'=>ijson_encode($remark),
					'createtime'=>TIMESTAMP,
					'status'=>1,
					'type'=>$type,
				);
				$this->insert('commission',$new_commission);
		}
		return true;
	}
	/*
		教育基金
	*/
	public function create_gu2_d($member,$type=1){
		$agent_id=$member['mid'];
		if(!$member || !$member['is_agent'] || $member['stop']){
			return $member['agent_id'];
		}
		if($type==1){
			if($member['type']==2){
				$rate=$this->setting['gu2_c'];
			}elseif($member['type']==3){
				$rate=$this->setting['gu3_c'];
			}elseif($member['type']==4){
				$rate=$this->setting['gu4_c'];
			}
		}
		if(!$rate){
			return $member['agent_id'];
		}
		$money=0;
		$order_price=$this->commission_order['commission_price'];
		$remark=array(
				'order_price'=>$order_price,
				'goods_commission'=>array(),
		);	
		$this->order_price=0;
		$commission_pv=$this->last_3fee1+$this->last_3fee2;
		if(substr($rate,-1)=='%'){
			$goods_pvmoney=($commission_pv*substr($rate,0,-1))/100;
			$money +=$goods_pvmoney;
		}else{
			$money +=$rate;
		}
		if($type==1){
			$money=$money-$this->gu_fee1;
			$this->gu_fee1 +=$money;
		}
		$remark['commission_money']=$money;
		if($money>0){
				$money=$this->up_fufee($member['mid'],$money);
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$member['mid'],
					'level'=>1,
					'ordersn'=>$this->order['ordersn'],
					'money'=>$money,
					'remark'=>ijson_encode($remark),
					'createtime'=>TIMESTAMP,
					'status'=>1,
					'type'=>$type,
				);
				$this->insert('commission',$new_commission);
		}
		return true;
	}
	/*
		分红池
	*/
	public function create_gu2_red($member,$type=0){
		$agent_id=$member['mid'];
		if(!$member || !$member['is_agent'] || $member['stop']){
			return $member['agent_id'];
		}
		if($member['type']==2 || $member['type']==5){
				$rate=$this->setting['gu2_red'];
		}elseif($member['type']==3 || $member['type']==6){
				$rate=$this->setting['gu3_red'];
		}elseif($member['type']==4 || $member['type']==7){
				$rate=$this->setting['gu4_red'];
		}
		if(!$rate){
			return $member['agent_id'];
		}
		$money=0;
		$order_price=$this->commission_order['commission_price'];
		$remark=array(
				'order_price'=>$order_price,
				'goods_commission'=>array(),
		);	
		$this->order_price=0;
		$commission_pv=0;
		foreach($this->order['goods'] as $goods){
				$goods['api_data']=json_decode($goods['api_data'],true);
				$pv=$goods['api_data']['pv'];
				$goods['total']=$goods['total']?:1;
				$commission_pv +=$pv*$goods['total'];
		}
		if(substr($rate,-1)=='%'){
			$goods_pvmoney=($commission_pv*substr($rate,0,-1))/100;
			$money +=$goods_pvmoney;
		}else{
			$money +=$rate;
		}
		if($money>0){
			$money=$money/3;
			$params=array('type'=>array(2,5),'is_agent'=>1,'stop'=>0,'uniacid'=>$_SESSION['uniacid']);
			$member2num=$this->count('member',$params);
			if($member2num>0){
				$member2=$this->getall('member',$params);
				$onefee=$money/$member2num;
				foreach($member2 as $mv){
					$onefee=$this->up_fufee($mv['mid'],$onefee);
					$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$mv['mid'],
						'level'=>1,
						'ordersn'=>$this->order['ordersn'],
						'money'=>$onefee,
						'remark'=>ijson_encode($remark),
						'createtime'=>TIMESTAMP,
						'status'=>1,
						'type'=>$type,
					);
					$this->insert('commission',$new_commission);
				}
			}
			$params['type']=array(3,6);
			$member3num=$this->count('member',$params);
			if($member3num>0){
				$member3=$this->getall('member',$params);
				$onefee=$money/$member3num;
				foreach($member3 as $mv){
					$onefee=$this->up_fufee($mv['mid'],$onefee);
					$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$mv['mid'],
						'level'=>1,
						'ordersn'=>$this->order['ordersn'],
						'money'=>$onefee,
						'remark'=>ijson_encode($remark),
						'createtime'=>TIMESTAMP,
						'status'=>1,
						'type'=>$type,
					);
					$this->insert('commission',$new_commission);
				}
			}
			$params['type']=array(5,7);
			$member4num=$this->count('member',$params);
			if($member4num>0){
				$member4=$this->getall('member',$params);
				$onefee=$money/$member4num;
				foreach($member4 as $mv){
					$onefee=$this->up_fufee($mv['mid'],$onefee);
					$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$mv['mid'],
						'level'=>1,
						'ordersn'=>$this->order['ordersn'],
						'money'=>$onefee,
						'remark'=>ijson_encode($remark),
						'createtime'=>TIMESTAMP,
						'status'=>1,
						'type'=>$type,
					);
					$this->insert('commission',$new_commission);
				}
			}
		}
		return true;
	}
	/*
		扶持基金
	*/
	public function up_fufee($mid,$money=0){
		$isboss2=$this->model('member')->isboss2($mid);
		if(!$isboss2){
			$fu_fee=$money*20/100;
			$money=$money*80/100;
			$this->update('member',array('fu_fee +='=>$fu_fee),array('mid'=>$mid));
			$feedata=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$mid,
				'fee'=>$fu_fee,
				'type'=>0,
				'status'=>0,
				'createtime'=>TIMESTAMP,
			);
			$this->insert('upfee',$feedata);
		}
		return $money;
	}
	/*
		订单完成时，把佣金状态由初始的订单待完成升级为待结算
	*/
	public function order_complete($order){
		$this->update('commission',array('status'=>1,'complete_time'=>TIMESTAMP),array('ordersn'=>$order['ordersn']));
	}
	/*
		创建结算单
	*/
	public function create_balance($mid,$post=array()){
		if(!$mid){
			exi('请先选择要结算的会员！','error');
		}
		if($post['pay_type']=='alipay'){
			if(!$post['realname']){
				exi('请先填写真实姓名！','error');
			}
			if(!$post['alipay']){
				exi('请先填写支付宝帐号！','error');
			}
			if(!$post['alipay1']){
				exi('请确认帐号','error');
			}
			if($post['alipay1']!=$post['alipay']){
				exi('确认帐号与支付宝帐号不匹配！','error');
			}
		}
		if($post['pay_type']=='union'){
			if(!$post['realname']){
				exi('请输入姓名','error');
			}
			if(!$post['bankname']){
				exi('请输入所属银行','error');
			}
			if(!$post['bankcard']){
				exi('请银行卡号','error');
			}
			if(!$post['bankcard1']){
				exi('请确认卡号','error');
			}
			if($post['bankcard']!=$post['bankcard1']){
				exi('确认卡号与银行卡号不匹配！','error');
			}
		}
		//获取可结算佣金
		$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'status'=>1);
		if($this->setting['balance_day']){
			//结算天数
			$time=TIMESTAMP-intval($this->setting['balance_day'])*86400;
			$params['complete_time <']=$time;
		}
		$commissions=$this->getall('commission',$params);
		if(!$commissions){
			exi('没有可结算的佣金！','error');
		}
		$commission_num=count($commissions);
		$money=0;
		foreach($commissions as $commission){
			$money+=$commission['money'];
		}
		//结算最低佣金检测
		if($money<$this->setting['balance_money']){
			exi('可结算佣金太少，无法结算！','error');
		}
		$new_balance=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'balancesn'=>get_ordersn($this->tablename('commission'),'balancesn','AC'),
			'commission_num'=>$commission_num,
			'money'=>$money,
			'pay_type'=>$post['pay_type'],
			'realname'=>$post['realname'],
			'alipay'=>$post['alipay'],
			'bankname'=>$post['bankname'],
			'bankcard'=>$post['bankcard'],
			'createtime'=>TIMESTAMP,
			'status'=>0,
		);
		//结算手续费
		if($this->setting['balance_per']){
			$new_balance['balance_pay']=intval($new_balance['money']*$this->setting['balance_per'])/100;
		}else{
			$new_balance['balance_pay']=0;
		}
		$new_balance['final_money']=$new_balance['money']-$new_balance['balance_pay'];
		$this->insert('balance',$new_balance);
		$balance_id=pdo_insertid();
		foreach($commissions as $commission){
			$update=array(
				'status'=>2,
				'balancesn'=>$new_balance['balancesn'],
			);
			$this->update('commission',$update,array('id'=>$commission['id']));
		}
		return $balance_id;
	}
	/*
		审核结算单
	*/
	public function check_balance($balance_id,$status,$remark=''){
		$update=array(
			'status'=>$status,
			'check_time'=>TIMESTAMP,
			'remark'=>$remark,
		);
		$this->update('balance',$update,array('id'=>$balance_id));
	}
	/*
		获取下级订单数
	*/
	public function get_teamordernum($mid){
		$mids=$this->getall('member',array('agent_id'=>$mid),array('mid'),'mid');
		if(count($mids)){
			$mids=array_keys($mids);
			$buynums=pdo_count('jy_weishop_order',array('ispay'=>1,'mid'=>$mids,'uniacid'=>$_SESSION['uniacid']),'id');
		}
		return $buynums;
	}
	/*
		打款结算单
	*/
	public function pay_balance($balance_id,$pay_type='',$wxgid='',$pay_trade_no=''){
		$balance=$this->get('balance',array('id'=>$balance_id,'uniacid'=>$_SESSION['uniacid']));
		if(!$balance || $balance['status']!=1){
			exi('结算单不存在或者不符合打款条件，打款失败！','error');
		}
		$pay_types=array_keys($this->pay_types);
		if(!$pay_type || !in_array($pay_type,$pay_types)){
			//exi('未设置打款方式或者打款方式不合法，打款失败！','error');
		}
		$func_name='pay_balance_by_'.$pay_type;
		$trade_no=$this->$func_name($balance['mid'],$balance['final_money'],$balance['balancesn'],$wxgid);
		$update=array(
			'status'=>3,
			'pay_trade_no'=>$trade_no=='pay_trade_no'?$pay_trade_no:$trade_no,
			'pay_time'=>TIMESTAMP,
		);
		if($pay_type){
			$update['pay_type']=$pay_type;
		}
		$this->update('balance',$update,array('id'=>$balance['id']));
		return true;
	}
	/*
		打款到余额
	*/
	protected function pay_balance_by_credit($mid,$money,$balancesn){
		return cfc('core')->member_credit($mid,'credit2',$money,true,$this->plugin_name,$remark='分销佣金打款');
	}
	/*
		打款到微信零钱
	*/
	protected function pay_balance_by_wechat($mid,$money,$balancesn,$wxgid=''){
		if($wxgid){
			$wxapp=pdo_get('core_account_wxapp',array('id'=>$wxgid,'uniacid'=>$_SESSION['uniacid']),array('appid','mchid','paykey'));
		}
		$params=array(
			'appid'=>$wxapp['appid']?:'',
			'mch_id'=>$wxapp['mchid']?:'',
			'openid'=>$mid,
			'mch_billno'=>$balancesn,
			'money'=>$money,
			'wxgid'=>$wxgid,
			'remark'=>'代理商佣金发放',
		);
		$res=cfc('wechat_pay')->mch_pay($params);
		return $res['payment_no'];
	}
	/*
		打款到支付宝
	*/
	protected function pay_balance_by_alipay($mid,$money,$balancesn){
		return 'pay_trade_no';
		exi('暂不支持打款到支付宝！','error');
	}
	/*
		打款到银行账户
	*/
	protected function pay_balance_by_union($mid,$money,$balancesn){
		return 'pay_trade_no';
		exi('暂不支持打款到银行账户！','error');
	}

	public function commission_remark($remark){
		if(!$remark){
			return '';
		}
		$remark=json_decode($remark,true);
		$goods_str='';
		if($remark['goods_commission']){
			foreach($remark['goods_commission'] as $goods){
				$goods['title']=pdo_getcolumn('jy_weishop_goods',array('id'=>$goods['goods_id']),'title');
				$buynum=$goods['buynum'];
				$goods_str.="商品ID【{$goods['goods_id']}】，商品名称【{$goods['title']}】，商品数量【{$goods['total']}】，商品总价【{$goods['total_price']}】，第【{$goods['buynum']}】次购买，佣金规则【{$goods['commission_per']}】，分销佣金【{$goods['commission_money']}】\n";
			}
		}else{
			$goods_str='未启用';
		}
		$remark['level_title']=pdo_getcolumn('jy_shopnewfee_level',array('uniacid'=>$_SESSION['uniacid'],'level'=>$remark['commission_level']),'title');
		$remark['level_title']=$remark['level_title']?:'默认等级';
		$remark_str=<<<TPL
分销商等级ID【{$remark['level_id']}】，等级名称【{$remark['level_title']}】，佣金等级【{$remark['commission_level']}】，订单总价【{$remark['order_price']}】，第【{$buynum}】次购买，
商品独立佣金：
{$goods_str}
订单佣金：
除去设置独立佣金的商品外订单金额【{$remark['order_commission']['order_price']}】，佣金规则【{$remark['order_commission']['commission_per']}%】，分销佣金【{$remark['order_commission']['commission_money']}】
佣金总计：【{$remark['commission_money']}】
TPL;
		return $remark_str;
	}
	/*
		获取单独商品佣金
	*/
	protected function get_goodsfee($goodsid,$orderid,$ordergoodsid=''){
		$ordersn=pdo_getcolumn('jy_weishop_order',array('id'=>$orderid,'uniacid'=>$_SESSION['uniacid']),'ordersn');
		$commissionremark=$this->pdo_getcolumn('commission',array('ordersn'=>$ordersn,'uniacid'=>$_SESSION['uniacid']),'remark');
		//print_r($ordersn.'~~'.$commissionremark);exit;
		$commissionremark=json_decode($commissionremark,true);
		
		$goodsfee=0;
		if($commissionremark['goods_commission']){
			foreach($commissionremark['goods_commission'] as $gv){
				if($ordergoodsid){
					if($ordergoodsid==$gv['ordergoodsid']){
						$goodsfee +=$gv['commission_money'];
					}
				}else{
					if($goodsid==$gv['goods_id']){
						$goodsfee +=$gv['commission_money'];
					}
				}
			}
		}
		if($commissionremark['order_commission'] && $goodsfee==0){
			$allorderfee=pdo_getsum('jy_weishop_order_goods',array('orderid'=>$orderid,'goodsid'=>$goodsid),'total_price');
			//exi($allorderfee.'~'.$commissionremark['order_commission']['order_price'].'~'.$commissionremark['order_commission']['commission_money'],'error');
			$goodsfee=($allorderfee/$commissionremark['order_commission']['order_price'])*$commissionremark['order_commission']['commission_money'];
		}
		return $goodsfee;
	}
}